package com.zhaohang.cn.zhaohangapp;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zhaohang.cn.constant.ConstantUtils;
import com.zhaohang.cn.uimarger.UiChengeAuto;
import com.zhaohang.cn.utils.AppManager;
import com.zhy.autolayout.AutoLinearLayout;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者: 赵航
 * 创建时间: 2018/10/14 13:29
 * 描述:
 */
public abstract class BasicActivity extends BasicAllActivity {

    private SmartRefreshLayout mSmartRefreshLayout;
    private LayoutInflater     mInflater;
    private ViewGroup          mView;
    private AutoLinearLayout   actionbar;
    private AutoLinearLayout   handlinear;
    private AutoLinearLayout   contentLayout;
    private RelativeLayout     left;
    private TextView           titletext;
    private RelativeLayout     right;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_basic);
        actionbar = (AutoLinearLayout) findViewById(R.id.actionbar);
        handlinear = (AutoLinearLayout) findViewById(R.id.handlinear);
        contentLayout = (AutoLinearLayout) findViewById(R.id.content_layout);
        left = (RelativeLayout) findViewById(R.id.left);
        titletext = (TextView) findViewById(R.id.titletext);
        right = (RelativeLayout) findViewById(R.id.right);

        AppManager.getInstance().addActivity(BasicActivity.this);
        initView();
        showState(setShowState());
    }

    private void initView() {
        mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        //初始化view
        if (null != contentLayout) {
            mView = (ViewGroup) mInflater.inflate(setContentSuccessView(), null);
            LinearLayout.LayoutParams mLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT);
            contentLayout.addView(mView, mLayoutParams);
            initChildView();
            ViewGroup mNoNetView = mView.findViewById(setNoNetView());


            //如果在无网状态下
            if (!isConnected()) {
                //指定无网络在哪个布局上进行显示
                if (mNoNetView != null) {
                    View mChild = mInflater.inflate(R.layout.nonet_layout, null);
                    LinearLayout.LayoutParams mChildParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                            LinearLayout.LayoutParams.MATCH_PARENT);
                    mNoNetView.addView(mChild, mChildParams);
                    for (int i = 0; i < mNoNetView.getChildCount(); i++) {
                        mNoNetView.getChildAt(i).setVisibility(View.GONE);
                    }
                    mChild.setVisibility(View.VISIBLE);

                    initNoNetView(mChild, mNoNetView);
                }
            } else {

                showViewWitheData();
                initData();
            }
        }
    }



    /**
     * 无网络的时候对无网界面进行操作，目前设置只可以下拉刷新，上拉加载更多，暂时不可用
     *
     * @param child
     * @param noNetView
     */
    private void initNoNetView(final View child, final ViewGroup noNetView) {
        mSmartRefreshLayout = (SmartRefreshLayout) child.findViewById(R.id.refreshLayout);
        //下拉刷新
        mSmartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                //                mSmartRefreshLayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
                if (!isConnected()) {
                    mSmartRefreshLayout.finishRefresh();
                    return;
                } else {
                    noNetView.removeView(child);
                    for (int i = 0; i < noNetView.getChildCount(); i++) {
                        noNetView.getChildAt(i).setVisibility(View.VISIBLE);
                    }
                    mSmartRefreshLayout.finishRefresh();

                    showViewWitheData();
                }
            }
        });
    }

    /**
     * @return 加载页面的布局
     */
    protected abstract int setContentSuccessView();

    /**
     * @return 怎么显示布局，是整体的还是局部的
     */
    protected abstract int setShowState();

    /**
     * 初始化view
     */

    protected abstract void initChildView();

    /**
     * @return 当有网络时，分为数据加载中，数据加载失败，数据加载成功三个界面，只有初始化的时候才进行显示
     */

    protected abstract int setNoDataView();

    /**
     * @return 设置数据
     */

    protected abstract Object setData();

    /**
     * @return 在父布局的那个ViewGrop中显示无网状态的布局
     */
    protected abstract int setNoNetView();

    /**
     * 初始化数据
     */
    protected abstract void initData();

    /**
     * @param showStyle 状态一位全背景，状态二为非全背景
     */
    private void showState(int showStyle) {
        switch (showStyle) {
            case ConstantUtils.LINEARSHOW:
                actionbar.setVisibility(View.VISIBLE);
                handlinear.setVisibility(View.VISIBLE);
                break;
            case ConstantUtils.RELEATSHOW:
                actionbar.setVisibility(View.GONE);
                handlinear.setVisibility(View.GONE);
                break;
        }
    }

    //当有网络时，分为数据加载中，数据加载失败，数据加载成功三个界面，只有初始化的时候才进行显示
    private void showViewWitheData() {

        final View view = mView.findViewById(setNoDataView());
        if (view != null) {
            UiChengeAuto chengeAuto = new UiChengeAuto(BasicActivity.this) {
                @Override
                protected View getSuccessView() {
                    mView.removeView(view);
                    return  view;
                }
                @Override
                protected Object getNetData() {
                    return setData();
                }
            };
            mView.addView(chengeAuto);
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        AppManager.getInstance().finishActivity(this); //从栈中移除
    }

    /**
     * @return 线性布局的头布局
     */
    public LinearLayout getHandLinear() {
        if (handlinear == null) {
            handlinear = (AutoLinearLayout) findViewById(R.id.handlinear);
        }
        return handlinear;
    }
    /**
     * 获取最顶部导航栏
     */
    public LinearLayout getActionLinear(){
        if (actionbar==null){
            actionbar = (AutoLinearLayout) findViewById(R.id.actionbar);
        }
        return actionbar;
    }

    /**
     * 设置左边按钮里是文字 setLeftShow(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选)
     *
     * @throws
     * @since 1.0.0
     */
    public void setLeftShow(int tag, int resID) {
        if (null == left) {
            left = (RelativeLayout) findViewById(R.id.left);
        }
        left.removeAllViews();
        left.setVisibility(View.VISIBLE);
        if (tag == 0) {
            View view = getLayoutInflater().inflate(R.layout.textview, null);
            TextView text = view.findViewById(R.id.text);
            text.setText(this.getResources().getString(resID));
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            left.addView(view, params);
        } else if (tag == 1) {
            View view = getLayoutInflater().inflate(R.layout.imageview, null);
            ImageView imageView = view.findViewById(R.id.icon);
            imageView.setImageResource(resID);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

            left.addView(view, params);
        } else if (tag == 3) {
            View view = getLayoutInflater().inflate(R.layout.weightback, null);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

            left.addView(view, params);
        }
    }

    public void setTitleText(String title) {

        if (titletext==null){
            titletext = (TextView) findViewById(R.id.titletext);
        }
        titletext.setText(title);
    }
    /**
     * 设置右边边按钮里是文字 setRightShow(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选)
     *
     * @throws
     * @since 1.0.0
     */
    public void setRightShow(int tag, int resID) {
        if (null == right) {
            right = (RelativeLayout) findViewById(R.id.right);
        }
        right.removeAllViews();
        right.setVisibility(View.VISIBLE);
        if (tag == 0) {
            View view = getLayoutInflater().inflate(R.layout.textview, null);
            TextView text = view.findViewById(R.id.text);
            text.setText(this.getResources().getString(resID));
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            right.addView(view, params);
        } else if (tag == 1) {
            View view = getLayoutInflater().inflate(R.layout.imageview, null);
            ImageView imageView = view.findViewById(R.id.icon);
            imageView.setImageResource(resID);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            right.addView(view, params);
        }
    }

}
